package com.lyh.web.controller.app;

import com.lyh.common.annotation.Log;
import com.lyh.common.core.controller.BaseController;
import com.lyh.common.core.domain.AjaxResult;
import com.lyh.common.core.page.TableDataInfo;
import com.lyh.common.enums.BusinessType;
import com.lyh.common.utils.StringUtils;
import com.lyh.common.utils.poi.ExcelUtil;
import com.lyh.system.domain.Asset;
import com.lyh.system.domain.vo.AnalogRechargeVo;
import com.lyh.system.domain.vo.AssetPublicityVo;
import com.lyh.system.mapper.AssetMapper;
import com.lyh.system.service.IAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 资产Controller
 *
 * @author ruoyi
 * @date 2023-04-27
 */
@Api(value = "充值",description = "充值")
@RestController
@RequestMapping("/app/asset")
public class AssetController extends BaseController {
    @Autowired
    private IAssetService assetService;
    @Autowired
    private AssetMapper assetMapper;

    /**
     * 查询资产列表
     */
    @PreAuthorize("@ss.hasPermi('system:asset:list')")
    @GetMapping("/list")
    public TableDataInfo list(Asset asset) {
        startPage();
        List<Asset> list = assetService.selectAssetList(asset);
        return getDataTable(list);
    }

    /**
     * 导出资产列表
     */
    @PreAuthorize("@ss.hasPermi('system:asset:export')")
    @Log(title = "资产", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Asset asset) {
        List<Asset> list = assetService.selectAssetList(asset);
        ExcelUtil<Asset> util = new ExcelUtil<Asset>(Asset.class);
        util.exportExcel(response, list, "资产数据");
    }

    /**
     * 获取资产详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:asset:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(assetService.selectAssetById(id));
    }

    /**
     * 新增资产
     */
    @PreAuthorize("@ss.hasPermi('system:asset:add')")
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Asset asset) {
        return toAjax(assetService.insertAsset(asset));
    }

    /**
     * 修改资产
     */
    @PreAuthorize("@ss.hasPermi('system:asset:edit')")
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Asset asset) {
        return toAjax(assetService.updateAsset(asset));
    }

    /**
     * 删除资产
     */
    @PreAuthorize("@ss.hasPermi('system:asset:remove')")
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assetService.deleteAssetByIds(ids));
    }


    /**
     * 资产通证转让
     */
    @Log(title = "资产通证转让", businessType = BusinessType.UPDATE)
    @PostMapping("/evidenceSend")
    public AjaxResult evidenceSend(
            @RequestParam(required = false) Long receiveMemberId,
                                   @RequestParam(required = false) BigDecimal num) {
        return success(assetService.send(receiveMemberId, num));
    }

    /**
     * 消费券转让
     */
    @Log(title = "消费券转让", businessType = BusinessType.UPDATE)
    @PostMapping("/consumerSend")
    public AjaxResult consumerSend(
            @RequestParam(required = false) Long receiveMemberId,
                                   @RequestParam(required = false) BigDecimal num) {
        return success(assetService.consumerSend(receiveMemberId, num));
    }

    /**
     * 门店积分
     */
    @Log(title = "门店积分", businessType = BusinessType.UPDATE)
    @PostMapping("/integralSend")
    public AjaxResult integralSend(@RequestParam(required = false) Long receiveMemberId,
                                   @RequestParam(required = false) Integer num) {
        return success(assetService.integralSend( receiveMemberId, num));
    }

    /**
     * 消费券充值
     */
    @Log(title = "消费券充值", businessType = BusinessType.UPDATE)
    @PostMapping("/consumerRecharge")
    public AjaxResult consumerRecharge(@RequestParam(required = false) Integer num) {
        return success(assetService.consumerRecharge(num));
    }

    /**
     * 资产公示
     */
    @GetMapping("/assetPublicity")
    public AjaxResult assetPublicity() {
        AssetPublicityVo assetPublicityVo = assetMapper.assetPublicity();
        return success(assetPublicityVo);
    }

    /**
     * 模拟充值
     * @param arv
     * @return
     */
    @PostMapping("/analogRecharge")
    @ApiOperation("充值-充值")
    public AjaxResult analogRecharge(@RequestBody AnalogRechargeVo arv){
        String s = assetService.newAnalogRecharge(arv);
        if(StringUtils.isNotEmpty(s)){
            return AjaxResult.success(s);
        }
        return AjaxResult.error();

    }


}
